#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import redis
import pymysql
import json



def process_item():
    """docstring for process_item"""

    rediscli = redis.Redis(host='127.0.0.1', port='6379')

    mysqlcli = pymysql.connect(
        user='lyh', passwd='123', db='zhenai')

    # 读取redis数据库内容

    offset = 0
    while True:

        source, data = rediscli.blpop("umsg:items")
        item = json.loads(data)

        # 使用游标读取数据库
        cursor = mysqlcli.cursor()

        try:
            sql = "insert into zhen(username, age, header_url, content, place_from, education, hobby, source_url, soure, time, spidername) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

            env = [item['username'].encode('utf-8'), item['age'].encode('utf-8'), item['header_url'].encode('utf-8'), item['content'].encode('utf-8'), item['place_from'], item['education'].encode('utf-8'), item['hobby'].encode('utf-8'), item['source_url'].encode('utf-8'), item['sourec'].encode('utf-8'), '2019-3-21', 'umsg']

            cursor.execute(sql, env)
        except Exception as e:
            continue
        else:
            mysqlcli.commit()
            mysqlcli.close()
            offset += 1
        print(offset)



if __name__ == '__main__':
    process_item()
